<template>
  <div class="co_text">
    <div class="co_text_box">
      <div class="co_text_title">{{co_text_html.title}}</div>
      <div
        class="co_text_content"
        v-html="co_text_html.content"
      ></div>
    </div>
    <div
      class="co_text_operation"
      @touchstart.self='co_text_touchstart'
      @touchend.self='co_text_touchend'
    >
      <div
        class="co_text_top"
        :style="'top:' + co_text_top + 'px;'"
      >{{co_text_html.title}}</div>
      <div
        class="co_text_bottom"
        :style="'bottom:' + co_text_bottom + 'px'"
      >
        <div class="co_text_chapter">
          <div>
            <div
              v-if="co_text_up"
              @click="kc_up"
            >上一章</div>
            <div
              v-else
              style="color: #666666;"
            >上一章</div>
          </div>
          <div>
            <div
              v-if="co_text_ne"
              @click="kc_ne"
            >下一章</div>
            <div
              v-else
              style="color: #666666;"
            >下一章</div>
          </div>
        </div>
        <div class="co_text_btn">
          <div @click="Buttondown">
            <img
              src="https://o6wndwjxn.qnssl.com/%E5%9B%BE%E6%A0%87%E4%B8%80@2x.png"
              alt=""
            >
            <div>后台播放</div>
          </div>
          <div @click="ButtonShare">
            <img
              src="https://o6wndwjxn.qnssl.com/share.svg"
              alt=""
            >
            <div>分享</div>
          </div>
          <div @click="tw_shoucang">
            <img
              v-if="tw_sc_status"
              src="https://o6wndwjxn.qnssl.com/Not%20to%20collect.svg "
              alt=""
            >
            <img
              v-else
              src="https://o6wndwjxn.qnssl.com/ysc.svg"
              alt=""
            >
            <div>收藏</div>
          </div>
          <div @click="co_text_left_cata">
            <img
              src="https://o6wndwjxn.qnssl.com/comments.svg"
              alt=""
            >
            <div>评论</div>
          </div>
        </div>

      </div>
      <div
        class="co_text_left"
        :style="'left:' + co_text_left + 'px'"
        @click="co_text_left_click"
      >
        <img
          src="https://o6wndwjxn.qnssl.com/The%20list%20of.svg"
          alt=""
        >
      </div>
      <div
        class="co_text_right"
        :style="'right:' + co_text_right + 'px'"
        @click="co_text_right_click"
      >
        <img
          src="https://o6wndwjxn.qnssl.com/Introductionss.svg"
          alt=""
        >
      </div>
    </div>
    <!-- 组件引入 -->
    <div
      class="Catalog"
      v-show="Catalog_status"
      @click.self="Popup_close"
    >
      <Catalog
        :type='co_video_Catalog'
        @CatalogClose='CatalogClose'
        @CatalogInput='CatalogInput'
      ></Catalog>
    </div>
    <div
      class="Comment"
      v-show="Comment_status"
      @click.self="Popup_close"
    >
      <Comment
        :type='co_video_Comment'
        @CommentStatus='CommentStatus'
        @CommentZid='CommentZid'
        @PurStatus='PurStatus'
      ></Comment>
    </div>
    <div
      class="Introduce"
      v-show="Introduce_status"
      @click.self="Popup_close"
    >
      <Introduce
        :type='co_video_Introduce'
        @IntroduceClose='IntroduceClose'
      ></Introduce>
    </div>
    <div
      class="Purchase"
      v-show="Purchase_status"
    >
      <Purchase
        :type='cid'
        @PurchaseClose='PurchaseClose'
      ></Purchase>
    </div>
    <div
      class="Tips"
      v-show="co_audio_tips_status"
      @click="co_audio_tips_click"
    >
      <Tips :type='co_audio_tips'></Tips>
    </div>
    <div
      class="Buttonshare_box"
      v-show="Buttonshare_box"
      @click="Buttonshare_box_click"
    >
      <img
        src="http://o6wndwjxn.qnssl.com/60a3e201812271357334941.png"
        alt=""
      >
      <div>点击分享给好友或朋友圈!邀请好友一起听课</div>
    </div>
  </div>
</template>

<script>
import {
  mapState
} from 'vuex'
import {
  comment_list,
  class_list,
  button_gochapter,
  go_content,
  school_number,
  search_popularity,
  user_ke,
  collection_onclick,
  dqtimes
} from '@/api/co_text'
import Catalog from '@/components/curriculum/Catalog.vue'
import Comment from '@/components/curriculum/Comment.vue'
import Introduce from '@/components/curriculum/Introduce.vue'
import Purchase from '@/components/curriculum/Purchase.vue'
import Tips from '@/components/public/Tips.vue'
export default {
  computed: {
    ...mapState({
      app_id: state => state.app_id,
      user_id: state => state.member_id
    })
  },
  components: {
    Catalog,
    Comment,
    Introduce,
    Purchase,
    Tips
  },
  data () {
    return {
      curriculumIndex: 0,
      is_status: '',
      cid: 0,
      zid: 0,
      stime: 0,
      Buttonshare_box: false,
      co_text_html: '',
      tw_tou_sy: 0,
      tw_tou_sw: 0,
      tw_tou_ey: 0,
      tw_tou_ew: 0,
      co_text_top: 0,
      co_text_bottom: 0,
      co_text_left: 10,
      co_text_right: 10,
      tw_sc_status: true,
      co_text_up: true,
      co_text_ne: true,
      co_audio_tips_status: false,
      Catalog_status: false,
      Comment_status: false,
      Introduce_status: false,
      Purchase_status: false,
      co_audio_tips: '空空如也',
      co_video_Catalog: [],
      co_video_Comment: [],
      co_video_Introduce: '',

      cvlbselect: 0,
      co_video_status: false
    }
  },
  destroyed () {
    let requery = {
      app_id: this.app_id,
      user_id: this.user_id,
      curriculum_id: this.co_curriculum_id,
      stime: this.stime,
      etime: Date.parse(new Date()) / 1000
    }
    // console.log('离开请求',requery)
    dqtimes(requery).then(res => { })
  },
  created () {
    this.curriculumIndex = this.$route.query.c
    // if (this.$route.query.status == 1) {
    //   this.co_video_status = true
    //   this.cvlbselect = this.$route.query.c
    // } else {
    //   this.co_video_status = false
    //   this.cvlbselect = this.$route.query.c
    // }
    this.cid = this.$route.query.curriculum_id
    this.zid = this.$route.query.chapter_id
    this.getDetails()
    this.getCatalog()
    this.getComment()
    this.getStudy()
    this.getCollection()
    this.stime = Date.parse(new Date()) / 1000
  },
  methods: {
    Buttondown () {
      if (window.__wxjs_is_wkwebview !== true) {
        window.location.href = 'https://a.app.qq.com/o/simple.jsp?pkgname=com.zhongtian.zhiyun'
      } else {
        window.location.href = `/xiazai?has=true`
      }
    },
    Buttonshare_box_click () {
      this.Buttonshare_box = false
    },
    ButtonShare () {
      this.Buttonshare_box = true
    },
    Introduce_status_click () {
      this.Introduce_status = true
    },
    getDetails (idx) {
      let requery = {
        app_id: this.app_id,
        curriculum_id: this.cid,
        chapter_id: this.zid,
        user_id: this.user_id
      }
      button_gochapter(requery).then(res => {
        this.$store.commit('updateLoadingStatus', { isLoading: false })
        let { code, data } = res.data
        // console.log('章节详情',data)
        if (code == 200) {
          this.is_status = data.is_status
          if (this.is_status == 1) {
            this.co_text_up = false
            this.co_text_ne = false
          }
          this.co_text_html = data
          this.co_video_Introduce = data.chapter_introduction
        } else {
          if (idx) {
            if (idx === 2) {
              this.curriculumIndex = this.curriculumIndex - 1
            } else {
              this.curriculumIndex = this.curriculumIndex + 1
            }
          }
          this.Purchase_status = true
        }
      })
    },
    getCatalog () {
      let requery = {
        app_id: this.app_id,
        curriculum_id: this.cid
      }
      comment_list(requery).then(res => {
        let {
          code,
          data
        } = res.data
        // console.log('全部评论',data)
        if (code == 200) {
          this.co_video_Catalog = data
        }
      })
    },
    getComment () {
      let requery = {
        app_id: this.app_id,
        curriculum_id: this.cid
      }
      class_list(requery).then(res => {
        // console.log('章节列表',res)
        let {
          code,
          data
        } = res.data
        if (code == 200) {
          for (var i = 0; i < data.length; i++) {
            data[i].status = false
            if (this.co_video_status) {
              data[i].audition = 1
            }
            if (data[i].chapter_id == this.zid) {
              data[i].status = true
            }
          }
          this.co_video_chapter = data.length
          this.co_video_Comment = data
        }
      })
    },
    CatalogInput (e) {
      let requery = {
        app_id: this.app_id,
        user_id: this.user_id,
        content: e,
        curriculum_id: this.cid
      }
      go_content(requery).then(res => {
        let {
          code,
          data
        } = res.data
        if (code == 200) {
          this.getCatalog()
        }
      })
    },
    tw_shoucang () {
      if (!this.tw_sc_status) return
      let params = {
        app_id: this.app_id,
        user_id: this.user_id,
        curriculum_id: this.cid
      }
      collection_onclick(params).then(res => {
        // console.log(res)
        let {
          code,
          data,
          msg
        } = res.data
        if (code === 200) {
          this.tw_sc_status = false
        }
      })
    },
    getStudy () {
      let requery = {
        app_id: this.app_id,
        curriculum_id: this.cid
      }
      school_number(requery).then(res => {
        let { code, data } = res.data
        // console.log(data)
        if (code == 200) {
          this.co_video_number = data
        }
      })
    },
    getCollection () {
      let requery = {
        app_id: this.app_id,
        user_id: this.user_id,
        type: 3
      }
      user_ke(requery).then(res => {
        let { data } = res.data
        // console.log('收藏列表',data)
        for (var i = 0; i < data.length; i++) {
          if (data[i].curriculum_id == this.cid) {
            this.tw_sc_status = false
          }
        }
      })
    },
    co_text_left_click () {
      this.Comment_status = true
    },
    PurStatus () {
      this.Purchase_status = true
    },
    co_text_right_click () {
      this.Introduce_status = true
    },
    co_text_left_cata () {
      this.Catalog_status = true
    },

    co_text_touchstart (e) {
      this.tw_tou_sy = e.changedTouches[0].clientY
      this.tw_tou_sw = e.changedTouches[0].clientX
    },
    co_text_touchend (e) {
      this.tw_tou_ey = e.changedTouches[0].clientY
      this.tw_tou_ew = e.changedTouches[0].clientX
      if (this.co_text_top == 0) {
        this.co_text_top = -500
        this.co_text_bottom = -90
        this.co_text_left = -100
        this.co_text_right = -100
      } else {
        if (Math.abs(this.tw_tou_ey - this.tw_tou_sy) <= 10 && Math.abs(this.tw_tou_ew - this.tw_tou_sw) <= 10) {
          this.co_text_top = 0
          this.co_text_bottom = 0
          this.co_text_left = 10
          this.co_text_right = 1
        }
      }
    },
    co_video_lb_click (e, id) {
      this.zid = id
      this.cvlbselect = e
      for (var i = 0; i < this.co_video_Comment.length; i++) {
        this.co_video_Comment[i].status = false
        this.co_video_Comment[e].status = true
      }
      this.getDetails()
    },
    kc_up () {
      if (this.curriculumIndex - 1 >= 0) {
        this.curriculumIndex = this.curriculumIndex - 1
        this.zid = this.co_video_Comment[this.curriculumIndex].chapter_id
        this.getDetails(1)
      } else {
        this.co_audio_tips_status = true
        this.co_audio_tips = '没有上一课'
      }
      // if (!this.co_video_status) {
      //   if (this.co_video_Comment[this.cvlbselect - 1] == undefined) {
      //     this.cvlbselect = 0
      //     this.co_audio_tips_status = true
      //     this.co_audio_tips = '没有上一课'
      //     return
      //   }
      //   if (this.co_video_Comment[this.cvlbselect - 1] != undefined && this.co_video_Comment[this.cvlbselect - 1].audition == 2) {
      //     this.Purchase_status = true
      //     return
      //   }
      // }
      // this.cvlbselect--
      // if (this.co_video_Comment[this.cvlbselect] != undefined) {
      //   this.zid = this.co_video_Comment[this.cvlbselect].chapter_id
      //   for (var i = 0; i < this.co_video_Comment.length; i++) {
      //     this.co_video_Comment[i].status = false
      //     this.co_video_Comment[this.cvlbselect].status = true
      //   }
      // } else {
      //   this.cvlbselect = 0
      //   this.co_audio_tips_status = true
      //   this.co_audio_tips = '没有上一课'
      // }
    },

    kc_ne () {
      if (parseInt(this.curriculumIndex) + 1 <= this.co_video_Comment.length - 1) {
        this.curriculumIndex = parseInt(this.curriculumIndex) + 1
        this.zid = this.co_video_Comment[this.curriculumIndex].chapter_id
        this.getDetails(2)
      } else {
        this.co_audio_tips_status = true
        this.co_audio_tips = '没有下一课'
      }
      // if (!this.co_video_status) {
      //   if (this.co_video_Comment[this.cvlbselect + 1] == undefined) {
      //     this.cvlbselect = 0
      //     this.co_audio_tips_status = true
      //     this.co_audio_tips = '没有上一课'
      //     return
      //   }
      //   if (this.co_video_Comment[this.cvlbselect + 1] != undefined && this.co_video_Comment[this.cvlbselect + 1].audition == 2) {
      //     this.Purchase_status = true
      //     return
      //   }
      // }
      // this.cvlbselect++
      // if (this.co_video_Comment[this.cvlbselect] != undefined) {
      //   this.zid = this.co_video_Comment[this.cvlbselect].chapter_id
      //   for (var i = 0; i < this.co_video_Comment.length; i++) {
      //     this.co_video_Comment[i].status = false
      //     this.co_video_Comment[this.cvlbselect].status = true
      //   }
      //   this.getDetails()
      // } else {
      //   this.cvlbselect = this.co_video_Comment.length - 1
      //   this.co_audio_tips_status = true
      //   this.co_audio_tips = '没有下一课'
      // }
    },
    CommentZid (e) {
      this.co_video_lb_click(e[0], e[1])
    },
    co_audio_tips_click () {
      this.co_audio_tips_status = false
    },
    CatalogClose () {
      this.Popup_close()
    },
    CommentStatus () {
      this.Popup_close()
    },
    IntroduceClose () {
      this.Popup_close()
    },
    PurchaseClose () {
      this.Popup_close()
    },
    Popup_close () {
      this.Catalog_status = false
      this.Comment_status = false
      this.Introduce_status = false
      this.Purchase_status = false
    }
  }
}
</script>

<style>
.Buttonshare_box {
  width: 100vw;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.8);
  z-index: 500;
  overflow: hidden;
  text-align: center;
}
.Buttonshare_box > img {
  width: 57px;
  height: 203px;
  margin-top: 15px;
  margin-left: calc(100vw - 100px);
  margin-right: 43px;
}
.Buttonshare_box > div {
  display: inline-block;
  max-width: 90vw;
  padding: 15px 25px;
  font-size: 13px;
  color: white;
  background: #f29911;
  border-radius: 500px;
  margin-top: 15px;
}
.Tips {
  width: 100vw;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
}
.co_text_box {
  width: calc(100vw - 40px);
  margin-left: 20px;
}
.co_text_title {
  font-size: 16px;
  color: #1a1a1a;
  font-weight: bold;
  text-align: center;
  margin: 20px 0;
}
.co_text_content {
  font-size: 15px;
}
.co_text_operation {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
}
.co_text_top {
  position: absolute;
  left: 0;
  width: 100%;
  color: white;
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.8);
  transition: all 0.5s;
}
.co_text_bottom {
  position: absolute;
  height: 90px;
  width: 100vw;
  padding: 0 20px;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  transition: all 0.5s;
}
.co_text_left,
.co_text_right {
  position: absolute;
  bottom: 100px;
  width: 40px;
  height: 40px;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 50%;
  transition: all 0.5s;
  z-index: 10;
}
.co_text_left {
  left: 10px;
}
.co_text_right {
  right: 10px;
}
.co_text_left > img,
.co_text_right > img {
  width: 100%;
  height: 100%;
  transform: scale(0.5);
}
.co_text_left > img {
  transform: scale(0.5);
}

.co_text_chapter {
  height: 40px;
  display: flex;
  position: relative;
}
.co_text_chapter > div {
  flex: 1;
  color: white;
  font-size: 14px;
  text-align: center;
  line-height: 40px;
}
.co_text_chapter:after {
  position: absolute;
  content: "";
  width: 1px;
  height: 20px;
  top: 10px;
  left: calc(50% - 1px);
  background: white;
}
.co_text_btn {
  height: 50px;
  overflow: hidden;
  display: flex;
  border-top: 1px solid #ffffff;
}
.co_text_btn > div {
  flex: 1;
  text-align: center;
}
.co_text_btn > div img {
  margin-top: 5px;
  height: 25px;
}
.co_text_btn > div div {
  color: white;
  font-size: 12px;
  margin-top: 2px;
}
.Catalog,
.Comment,
.Introduce,
.Purchase {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.6);
  z-index: 100;
}
</style>
